package net.maku.system.erp.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.system.erp.convert.SysClientsConvert;
import net.maku.system.erp.entity.SysClientsEntity;
import net.maku.system.erp.query.SysClientsQuery;
import net.maku.system.erp.service.SysClientsService;
import net.maku.system.erp.vo.SysClientsVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
* 客户实体
*
* @author 王宝林 baolin.wang@aliyun.com
* @since 1.0.0 2024-03-22
*/
@RestController
@RequestMapping("erp/clients")
@Tag(name="客户实体")
@AllArgsConstructor
public class SysClientsController {
    private final SysClientsService sysClientsService;

    @GetMapping("page")
    @Operation(summary = "分页")
    public Result<PageResult<SysClientsVO>> page(@ParameterObject @Valid SysClientsQuery query){
        PageResult<SysClientsVO> page = sysClientsService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    public Result<SysClientsVO> get(@PathVariable("id") Long id){
        SysClientsEntity entity = sysClientsService.getById(id);

        return Result.ok(SysClientsConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    public Result<String> save(@RequestBody SysClientsVO vo){
        sysClientsService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    public Result<String> update(@RequestBody @Valid SysClientsVO vo){
        sysClientsService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    public Result<String> delete(@RequestBody List<Long> idList){
        sysClientsService.delete(idList);

        return Result.ok();
    }
}